package com.example.shop_2211.order

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.CreateOrderEntityParam
import com.example.base.mvvm.BaseActivity
import com.example.shop_2211.R
import com.example.shop_2211.adapter.OrderAdapter
import com.example.shop_2211.databinding.ActivityOrderBinding

@Route(path = "/app/OrderActivity")
class OrderActivity : BaseActivity<ActivityOrderBinding,OrderViewModel>() {
    @Autowired
    @JvmField
    var preOrderNo = ""
    var payType = "yue"
    lateinit var orderAdapter: OrderAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        orderAdapter = OrderAdapter()
        binding.rv.adapter = orderAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        viewModel.getPreOrder(preOrderNo)
        binding.btnPay.setOnClickListener {
            val createOrderEntityParam = CreateOrderEntityParam()
            createOrderEntityParam.preOrderNo = preOrderNo
            createOrderEntityParam.addressId = 71
            createOrderEntityParam.payChannel = "weixinh5"
            createOrderEntityParam.payType = payType
            createOrderEntityParam.shippingType = 1
            viewModel.createOrder(createOrderEntityParam)
        }
    }

    override fun initData() {
        viewModel.getSuccess.observe(this){
            if(it.code==200){
                binding.tvRealname.text = it.data.orderInfoVo.realName
                binding.tvPhone.text = it.data.orderInfoVo.phone
                binding.tvAddress.text = it.data.orderInfoVo.province+it.data.orderInfoVo.city+it.data.orderInfoVo.district+it.data.orderInfoVo.detail
                binding.tvCount.text = "共有${it.data.orderInfoVo.orderDetailList.size}件商品"

                //订单列表
                orderAdapter.data.clear()
                orderAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
                orderAdapter.notifyDataSetChanged()
                //合计金额
                binding.tvTotal.text = "合计：￥${it.data.orderInfoVo.proTotalFee}"

            }
        }
        viewModel.createSuccess.observe(this){
            ToastUtils.showLong("支付失败")
            //ToastUtils.showLong("$it")
            if(it.code==200){
                //进行真正的支付
                ARouter.getInstance().build("/app/MainActivity").withInt("index",2).navigation()
            }
        }
    }

    override fun getViewModel(): Class<OrderViewModel>  = OrderViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_order

}